package com.xingmao.account.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.security.utils.DictUtils;
import com.xingmao.account.domain.AccountInfoExcel;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.xingmao.account.domain.AccountInfo;
import com.xingmao.account.service.IAccountInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 账单信息Controller
 *
 * @author xingmao
 * @date 2024-06-01
 */
//@Api(value = "账单信息", description = "账单相关的API")
@RestController
@RequestMapping("/info")
public class AccountInfoController extends BaseController {
    @Autowired
    private IAccountInfoService accountInfoService;

    /**
     * 查询账单信息列表
     */
//    @ApiOperation(value = "查询账单信息列表")
    @RequiresPermissions("account:info:list")
    @GetMapping("/list")
    public TableDataInfo list(AccountInfo accountInfo) {
        startPage();
        List<AccountInfo> list = accountInfoService.selectAccountInfoList(accountInfo);
        return getDataTable(list);
    }

    /**
     * 导出账单信息列表
     */
    @RequiresPermissions("account:info:export")
    @Log(title = "账单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountInfo accountInfo) {
        List<AccountInfo> list = accountInfoService.selectAccountInfoList(accountInfo);
        List<AccountInfoExcel> accountInfoExcels = BeanUtil.copyToList(list, AccountInfoExcel.class);
        accountInfoExcels = accountInfoExcels.stream().peek(nowData -> {
            if (StrUtil.isNotEmpty(nowData.getTypeValue())) {
                nowData.setInfoType(
                        DictUtils.getDictLabelByKeyAndValueCache(
                                "account_info_type", nowData.getTypeValue()
                        )
                );
            } else {
                nowData.setInfoType("");
            }
        }).collect(Collectors.toList());
        ExcelUtil<AccountInfoExcel> util = new ExcelUtil<AccountInfoExcel>(AccountInfoExcel.class);
        util.exportExcel(response, accountInfoExcels, "账单信息数据");
    }

    /**
     * 获取账单信息详细信息
     */
    @RequiresPermissions("account:info:query")
    @GetMapping(value = "/{accountInfoId}")
    public AjaxResult getInfo(@PathVariable("accountInfoId") Long accountInfoId) {
        return success(accountInfoService.selectAccountInfoByAccountInfoId(accountInfoId));
    }

    /**
     * 新增账单信息
     */
    @RequiresPermissions("account:info:add")
    @Log(title = "账单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccountInfo accountInfo) {
        return toAjax(accountInfoService.insertAccountInfo(accountInfo));
    }

    /**
     * 修改账单信息
     */
    @RequiresPermissions("account:info:edit")
    @Log(title = "账单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccountInfo accountInfo) {
        return toAjax(accountInfoService.updateAccountInfo(accountInfo));
    }

    /**
     * 删除账单信息
     */
    @RequiresPermissions("account:info:remove")
    @Log(title = "账单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountInfoIds}")
    public AjaxResult remove(@PathVariable Long[] accountInfoIds) {
        return toAjax(accountInfoService.deleteAccountInfoByAccountInfoIds(accountInfoIds));
    }
}
